package com.djf.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.djf.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 用户Mapper接口
 * 用户表作为广播表，不分表
 *
 * @author djf
 * @since 2025-01-01
 */
@Mapper
public interface UserMapper extends BaseMapper<User> {

    /**
     * 根据租户ID查询用户列表
     *
     * @param tenantId 租户ID
     * @return 用户列表
     */
    @Select("SELECT * FROM t_user WHERE tenant_id = #{tenantId} AND deleted = 0")
    List<User> getUsersByTenantId(@Param("tenantId") Long tenantId);

    /**
     * 根据手机号查询用户
     *
     * @param phone 手机号
     * @return 用户信息
     */
    @Select("SELECT * FROM t_user WHERE phone = #{phone} AND deleted = 0")
    User getUserByPhone(@Param("phone") String phone);

    /**
     * 根据用户ID列表批量查询用户
     *
     * @param userIds 用户ID列表
     * @return 用户列表
     */
    @Select("<script>" +
            "SELECT * FROM t_user WHERE id IN " +
            "<foreach collection='userIds' item='userId' open='(' separator=',' close=')'>" +
            "#{userId}" +
            "</foreach>" +
            " AND deleted = 0" +
            "</script>")
    List<User> getUsersByIds(@Param("userIds") List<Long> userIds);

    /**
     * 根据状态查询用户列表
     *
     * @param status 用户状态
     * @return 用户列表
     */
    @Select("SELECT * FROM t_user WHERE status = #{status} AND deleted = 0")
    List<User> getUsersByStatus(@Param("status") Integer status);

    /**
     * 根据租户ID和状态查询用户列表
     *
     * @param tenantId 租户ID
     * @param status   用户状态
     * @return 用户列表
     */
    @Select("SELECT * FROM t_user WHERE tenant_id = #{tenantId} AND status = #{status} AND deleted = 0")
    List<User> getUsersByTenantIdAndStatus(@Param("tenantId") Long tenantId, @Param("status") Integer status);
}